﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Collections.Generic;
using Ccit.OA.MsQuestion;
using Newtonsoft.Json;
namespace Ccit.OA.Question
{
    public partial class qst_manage_index : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ViewState["Query"] = "";
                _hidTabId.Value = (null != Request.QueryString["TABID"]) ? Request.QueryString["TABID"].ToString() : "";
                _hidWhereClause.Value = Ccit.OA.Utils.SysInterface.GetPageDataRangeSQL("Qst_A1", _hidTabId.Value);
                if (_hidWhereClause.Value == "")
                {
                    _hidWhereClause.Value = "1=1";
                }
            }
            InitPage();
        }
        private void InitPage()
        {
            ViewState["Query"] = "";
            if (this.txttName.Text.Trim() != "")
            {
                ViewState["Query"] = " and $$title$$ like'%" + txttName.Text.Replace("'","''")+ "%'";
            }
            if(this.ddlStaue.SelectedValue!="4")
            {
                ViewState["Query"] += " and $$state$$ =" + ddlStaue.SelectedValue + "";
            }
            DataBinde();
        }
        private void DataBinde()
        {
            QuestionBLL Bll = new QuestionBLL();
            List<MsQuestionMes> lstMes = Bll.GetQuestionMes(ViewState["Query"].ToString(), _hidWhereClause.Value);
            dgMain.DataSource = lstMes;
            dgMain.DataBind();
        }

        protected void dgMain_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            string sRecid = dgMain.GetRowValues(e.VisibleIndex, "id").ToString();
            string state = dgMain.GetRowValues(e.VisibleIndex, "state").ToString();
            string sName = dgMain.GetRowValues(e.VisibleIndex, "title").ToString();
            string stype = dgMain.GetRowValues(e.VisibleIndex, "type").ToString();
            string ucount = dgMain.GetRowValues(e.VisibleIndex, "ucount").ToString();
            /**************************************************************************/ 
            e.Row.Cells[1].Text = "<a href=\"javascript:doView('" + sRecid + "')\">" + sName + "</a>"; 
            HtmlAnchor lnbEdit = (HtmlAnchor)dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbEdit");
            HtmlAnchor lnbDel = (HtmlAnchor)dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbDel");
            HtmlAnchor lnbCtrlC = (HtmlAnchor)dgMain.FindRowCellTemplateControl(e.VisibleIndex, null, "lnbCtrlC");
            lnbCtrlC.Attributes["href"] = "javascript:doCtrlv('" + sRecid + "')";
            lnbDel.Attributes["href"] = "javascript:doDel('" + sRecid + "')";
            /**************************************************************************/
            if(state=="0")
            {
                e.Row.Cells[2].Text = "未发布";
                lnbEdit.Attributes["href"] = "javascript:doEdit('" + sRecid + "')";
                e.Row.Cells[7].Text = "结果统计";
            }
            else if (state == "1")
            {
                e.Row.Cells[2].Text = "已发布";
                lnbEdit.Disabled = true;
                e.Row.Cells[7].Text = "<a href=\"javascript:doCount('" + sRecid + "')\">结果统计</a>";
            }
            else
            {
                e.Row.Cells[2].Text = "已结束";
                lnbEdit.Disabled = true;
                e.Row.Cells[7].Text = "<a href=\"javascript:doCount('" + sRecid + "')\">结果统计</a>";
            }
            if (stype == "1")
            {
                e.Row.Cells[3].Text = "实名";
                if(state!="0")
                    e.Row.Cells[4].Text = "<a href=\"javascript:doSelectInfo('" + sRecid + "')\">" + ucount + "</a>";
            }
            else
            { e.Row.Cells[3].Text = "匿名"; }
            /**************************************************************************/
           
        }

        protected void lnbQuery_Click(object sender, EventArgs e)
        {
            DevExpress.Web.Data.WebDataSelection oSelect = dgMain.Selection;
            oSelect.UnselectAll();
            InitPage();
        }
        #region Ajax Methods
        /// <summary>
        /// get budge 
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DoOperate(string strJson, string cType)
        {
            List<string> lstResult = new List<string>();
            MsQuestionMes ms = new MsQuestionMes();
           
            QuestionBLL Bll = new QuestionBLL();
            if (cType == "0")
            {
                ms = (MsQuestionMes)JavaScriptConvert.DeserializeObject(strJson, typeof(MsQuestionMes));
                lstResult = Bll.DoOperate("", ms, cType);
            }
            else
            {
                lstResult = Bll.DoOperate(strJson, null, cType);
            }
            var newObj = new {message = lstResult[1],resulttype=lstResult[0] };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr; 
        }
        #endregion
        #region Ajax Methods
        /// <summary>
        /// get budge 
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DoCopy(string sRecid)
        {
            QuestionBLL Bll = new QuestionBLL();
            List<string> lstResult = new List<string>();
            lstResult = Bll.DoCopyMes(sRecid);           
            var newObj = new { message = lstResult[1], resulttype = lstResult[0] };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }
        #endregion
    }
}
